const app = getApp();
import Api from '@/services/api.js'
/**
 * 我的页面 功能区
 */
const mineFunc = [{
  title: '企业认证',
  rightText: '试用版',
  iconWidth: '52',
  iconHeight: '48',
  isAuth: true,
  type: 'enterprise',
  pageUrl: '/enterpriseCert/home/index',
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/enterpriseCertification.png'
}, {
  title: '订单管理',
  iconWidth: '52',
  iconHeight: '48',
  isAuth: true,
  roles: ['boss', 'hr'],
  pageUrl: '/pages/order/index',
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/enterpriseCertification.png'
}, {
  title: '部门设置与管理',
  pageUrl: '/enterpriseMgmt/home/index',
  iconWidth: '44',
  iconHeight: '44',
  isAuth: true,
  roles: ['boss', 'hr'],
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/departmentSetting.png'
}, {
  title: 'HR登录',
  pageUrl: '/hr/home/index',
  iconWidth: '44',
  iconHeight: '44',
  isAuth: true,
  roles: ['other'],
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/departmentSetting.png'
}, {
  title: '分销中心',
  iconWidth: '50',
  iconHeight: '50',
  type: 'distribution',
  pageUrl: '/distribution/apply/index',
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/help.png'
}, {
  title: '使用帮助',
  iconWidth: '50',
  iconHeight: '50',
  type: 'useHelp',
  pageUrl: '/pages/common/description/index',
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/help.png'
}, {
  title: '工具包下载',
  iconWidth: '48',
  iconHeight: '48',
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/download.png'
}, {
  title: '个人中心',
  iconWidth: '48',
  iconHeight: '48',
  isAuth: true,
  pageUrl: '/pages/personalCenter/index',
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/mineCenter.png'
}, {
  title: '关于我们',
  iconWidth: '46',
  iconHeight: '46',
  type: 'aboutUs',
  pageUrl: '/pages/common/description/index',
  icon: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/mine/about.png'
}]
export default {
  data() {
    return {
      funcList: [],
      info: {
        vip: {}
      },
    }
  },
  computed: {
    // 是否是BOSS(有老板权限)
    isBoss() {
      return this.privacyData.roles.includes('boss')
    },
    // 是否是HR
    isHr() {
      return this.privacyData.roles.includes('hr')
    },
    // 权限列表
    roles() {
      return this.privacyData.roles;
    },
    privacyData() {
      return this.$store.state.privacyData;
    },
    companyInfo() {
      return this.$store.state.companyInfo;
    }
  },
  onLoad() {
  },
  onShow() {
    if (this.privacyData.openId) {
      this.init()
    } else {
      app.globalData.callBack = () => {
        this.init();
      }
    }
  },
  methods: {
    async init() {
      await this.getUserInfo();
      this.getFuncItemByRoles();
      // 没有企业id就是初始化 需要取第一条数据
      if (this.isBoss || this.isHr) {
        await this.$Serv.getEnterpriseList(this.companyInfo.enterpriseId ? 'self' : 'init')
      }
    },
    // 根据权限获取功能区域
    getFuncItemByRoles() {
      // 判断 角色权限和限定的权限有交集或者没有设权限的才展示
      this.funcList = mineFunc.reduce((p, n) => {
        const isContainRole = this.roles.some(i => (n.roles || []).includes(i));
        if (isContainRole || !n.roles) {
          p.push(n)
        }
        return p;
      }, []);
    },
    // 获取用户信息
    async getUserInfo() {
      const {
        code,
        result
      } = await this.$request({
        url: Api.getUserInfo,
      })
      if (code === 200) {
        this.info = result;
        this.$Serv.updatePrivacyData(result)
      }
    },
    // 功能区域点击事件
    handleFunc(e) {
      let {
        item,
        type
      } = e.currentTarget.dataset;
      if (this.$Serv.checkAuthLogin(item.isAuth)) {
        return
      }

      if (!item.pageUrl) return;
      if (type == 'enterprise' && this.isBoss) {
        item.pageUrl = '/enterpriseCert/enterpriseList/index'
      }
      if (type == 'distribution' && this.roles.includes('agentAudit')) {
        item.pageUrl = '/distribution/index'
      }
      uni.navigateTo({
        url: item.pageUrl + `?func=${JSON.stringify(item)}`,
        events: {
          refresh: ({ operate }) => {
            this.getUserInfo();
            if (operate == 'hrLogin') {
              this.$Serv.getEnterpriseList('init')
            }
          }
        }
      })
    },
    async shareAppraisal() {
      const ret = await this.$request({
        url: Api.getShareQrByType,
        method: 'GET',
        data: {
          type: 1,
          pagePath: 'pages/quizzes/index',
          enterpriseId: this.companyInfo.enterpriseId,
          enterpriseName: this.companyInfo.companyName
        }
      })
      if (ret.code == 200) {
        uni.previewImage({
          urls: [ret.result]
        })
      }
    }
  },
  // 分享
  /**
   * + '&enterpriseId=' + this.companyInfo
        .enterpriseId + '&enterpriseName=' + this.companyInfo.companyName
   */
  onShareAppMessage() {
    return {
      title: '人才匹配度测评工具',
      path: 'pages/mine/index?inviteId=' + this.privacyData.userId,
      imageUrl: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/default_2.png'
    }
  },
  async onPullDownRefresh() {
    this.init();
    // await this.getUserInfo(); //微信渠道授权登录
    // // 没有企业id就是初始化 需要取第一条数据
    // if (this.isBoss || this.isHr) {
    //   await this.$Serv.getEnterpriseList(this.companyInfo.enterpriseId ? 'self' : 'init')
    // }
    uni.stopPullDownRefresh();
  }
}